package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.OrganizationDto;
import com.qf.commerce.common.vo.OrganizationVo;
import com.qf.commerce.entity.Organization;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;

    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody OrganizationDto organizationDto){
        try {
            return ResponseResult.success(organizationService.insertOrganization(organizationDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
    @DeleteMapping("/delete")
    public ResponseResult<Integer> delete(@RequestParam List<Long> ids){
        try {
            return ResponseResult.success(organizationService.deleteOrganization(ids));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
    @DeleteMapping("/del")
    public ResponseResult<Integer> del(@RequestParam int status, @RequestParam Long organizationId){
        try {
            return ResponseResult.success(organizationService.del(status,organizationId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @PutMapping("/update")
    public ResponseResult<Integer> update(@RequestBody Organization organization){
        try {
            return ResponseResult.success(organizationService.updateOrganization(organization, organization.getOrganizationId()));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }


    @GetMapping("/listDetail")
    public ResponseResult<Organization> listDetail(@RequestParam Long organizationId){
        try {
            return ResponseResult.success(organizationService.selectOrganizationDetail(organizationId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @GetMapping("/list")
    public ResponseResult<PageInfo<OrganizationVo>> list(Integer status, String organizationName, String userName,Integer page, Integer size){
        try {
            return ResponseResult.success(organizationService.selectOrganization(status,organizationName,userName,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
    @GetMapping("/listByTime")
    public ResponseResult<PageInfo<OrganizationVo>> listByTime(String startCreateTime, String endCreateTime, Integer page, Integer size){
        try {
            return ResponseResult.success(organizationService.selectOrganizationByTime(startCreateTime,endCreateTime,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }


}
